<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('还本付息') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('投资主体')" prop="investmentId">
        <el-select v-model="queryParams.investmentId" :placeholder="$t('投资主体')">
          <el-option
            v-for="investment in investmentList"
            :key="investment.id"
            :label="investment.investorName"
            :value="investment.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('起息日')" prop="breathDay">
        <DatePicker v-model="dateRange" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:servicing:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:servicing:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="dealServicingList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('投资主体')" align="center" prop="investorName">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.investorName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('投资本金') + $t('金额单位')" align="center" prop="amount">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('年利率(%)')" align="center" prop="interestRate" width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.interestRate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('计息天数')" align="center" prop="interestDay" />
      <el-table-column :label="$t('起息日')" align="center" prop="valueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.valueDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('到期日')" align="center" prop="maturityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maturityDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:servicing:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:servicing:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目还本付息对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealServicingForm ref="dealServicingForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealServicing, delDealServicing, exportDealServicing } from '@/api/deal/servicing/dealServicing'
import dealServicingForm from './dealServicingForm.vue'
import { listDealInvestment } from '@/api/deal/investment/dealInvestment'
export default {
    components: {
        dealServicingForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目还本付息表格数据
          dealServicingList: [],
          // 弹出层标题
          title: '还本付息',
          // 是否显示弹出层
          formOpen: false,
        dealId: this.$route.query.id,
        // 项目投资主体列表
        investmentList: [],
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              investmentId: undefined,
              pageNum: 1,
              pageSize: 10
          },
        // 日期范围
        dateRange: [],
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      this.getList()
    listDealInvestment({ 'dealId': this.dealId }).then(response => {
      this.investmentList = response.rows
    })
  },
  methods: {
    /** 查询项目还本付息列表 */
    getList() {
        this.loading = true
        listDealServicing(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.dealServicingList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: this.$route.query.id,
            investmentId: undefined,
            amount: undefined,
            interestRate: undefined,
            interestDay: undefined,
            repaymentFrequency: undefined,
            repaymentInterval: undefined,
            repaymentAgreed: undefined,
            interestFrequency: undefined,
            interestInterval: undefined,
            interestAgreed: undefined,
            breathDay: undefined,
            maturityData: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealServicingForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealServicingForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealServicing(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportDealServicing(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
